package com.airsystem.pos.bengkel.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import com.airsystem.pos.bengkel.controller.MasterVehicleController;
import com.airsystem.pos.bengkel.controller.MenuController;
import com.airsystem.pos.bengkel.util.TextUtil;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class MasterVehicle extends MasterVehicleController {

	public MasterVehicle(MenuController menuController) {
		super();

		this.menuController = menuController;
		initComponents();
		setup();
	}

	private void initComponents() {
		setTitle("Data Kendaraan");
		setFrameIcon(new ImageIcon(getClass().getResource("/images/vehicle.png")));
		setIconifiable(true);
		setClosable(true);

		baseLayout.columnWidths = new int[] { 600, 0 };
		baseLayout.rowHeights = new int[] { 0, 0 };
		baseLayout.columnWeights = new double[] { 1.0, 0.0 };
		baseLayout.rowWeights = new double[] { 0.0, 1.0 };
		getContentPane().setLayout(baseLayout);

		panelLayout.columnWidths = new int[] { 0, 0, 0 };
		panelLayout.rowHeights = new int[] { 0, 0, 0 };
		panelLayout.columnWeights = new double[] { 0.0, 1.0, 0.0 };
		panelLayout.rowWeights = new double[] { 0.0, 0.0, 0.0 };
		basePanel.setLayout(panelLayout);

		merkLabel.setFont(new Font("Verdana", 0, 12));
		merkLabel.setText("Merk");

		submerkLabel.setFont(new Font("Verdana", 0, 12));
		submerkLabel.setText("Sub Merk");

		merkField.setFont(new Font("Verdana", 0, 12));
		merkField.setColumns(15);
		merkField.setDocument(new TextUtil(30, TextUtil.CAPITAL));

		submerkField.setFont(new Font("Verdana", 0, 12));
		submerkField.setColumns(15);
		submerkField.setDocument(new TextUtil(30, TextUtil.ALPHACAPITAL));

		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));

		searchButton.setIcon(new ImageIcon(getClass().getResource("/images/search.png")));
		searchButton.setFont(new Font("Verdana", 1, 12));
		searchButton.setText("CARI");

		addButton.setIcon(new ImageIcon(getClass().getResource("/images/add.png")));
		addButton.setFont(new Font("Verdana", 1, 12));
		addButton.setText("TAMBAH");
		buttonPanel.add(addButton);

		saveButton.setIcon(new ImageIcon(getClass().getResource("/images/save.png")));
		saveButton.setFont(new Font("Verdana", 1, 12));
		saveButton.setText("SIMPAN");
		buttonPanel.add(saveButton);

		updateButton.setIcon(new ImageIcon(getClass().getResource("/images/update.png")));
		updateButton.setFont(new Font("Verdana", 1, 12));
		updateButton.setText("UBAH");
		buttonPanel.add(updateButton);

		deleteButton.setIcon(new ImageIcon(getClass().getResource("/images/delete.png")));
		deleteButton.setFont(new Font("Verdana", 1, 12));
		deleteButton.setText("HAPUS");
		buttonPanel.add(deleteButton);

		cancelButton.setIcon(new ImageIcon(getClass().getResource("/images/cancel.png")));
		cancelButton.setFont(new Font("Verdana", 1, 12));
		cancelButton.setText("BATAL");
		buttonPanel.add(cancelButton);

		initVehicles();

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				searchButtonActionPerformed(evt);
			}
		});

		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});

		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				saveButtonActionPerformed(evt);
			}
		});

		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				updateButtonActionPerformed(evt);
			}
		});

		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		basePanel.add(merkLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 30, 20, 0), 0, 0));
		basePanel.add(submerkLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 30, 20, 0), 0, 0));
		basePanel.add(merkField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30, 20, 20, 0), 0, 0));
		basePanel.add(submerkField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(searchButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(30, 30, 20, 30), 0, 0));
		basePanel.add(buttonPanel, new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

		getContentPane().add(basePanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 600) / 2, (screenSize.height - 600) / 2, 600, 500);
	}

	private void initVehicles() {
		tablevehicleScroll.setViewportView(tableVehicle);
		tableVehicle.setFont(new Font("Verdana", 0, 12));
		tableVehicle.setModel(tableModel);

		tableVehicle.setPreferredScrollableViewportSize(new Dimension(150, 150));
		tableVehicle.setFillsViewportHeight(true);

		tableVehicle.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				tableVehicleMouseClicked(evt);
			}
		});

		getContentPane().add(tablevehicleScroll, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 1, 1, 1), 0, 0));
	}

	private void searchButtonActionPerformed(ActionEvent evt) {
		search();
	}

	private void tableVehicleMouseClicked(MouseEvent evt) {
		row = tableVehicle.getSelectedRow();

		if (evt.getClickCount() == 1 && tableVehicle.getRowCount() != 0) {
			init();
		}
	}

	private void addButtonActionPerformed(ActionEvent evt) {
		add();
	}

	private void saveButtonActionPerformed(ActionEvent evt) {
		save();
	}

	private void updateButtonActionPerformed(ActionEvent evt) {
		update();
	}

	private void deleteButtonActionPerformed(ActionEvent evt) {
		delete();
	}

	private void cancelButtonActionPerformed(ActionEvent evt) {
		cancel();
	}
}